<?php
/**
 * 用户表数据操作层
 * @author yangchao
 * @package user
 * @copyright yangchao
 * @version 1.0
 * @code-encode utf-8
 * @data-encode 2011-01-17
 */
define("CACHE_PREFIX_USER","_qq_user_info_");
class db_qq_user_info extends db_base {
    
	var $table_name = "qq_user_info";
	
	function db_qq_user_info()
	{
		parent::db_base();
	}
	
	/**
	 * 前台取用户信息
	 * @param $uid
	 */
	function get_qq_user_info_item_by_uid($uid)
	{
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER."get_qq_user_info_item_by_uid_{$uid}";
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$content = $this->get_alllist($this->table_name,1,1,"","uid='{$uid}' and status=1");
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		if($content)
		{
			foreach($content as $key => $value)
			{
				$content[$key]["user_pic"] = "http://tp1.sinaimg.cn/{$value["uid"]}/180/5635936750/1";
			}
		}
		return $content[0];
		
	}
	
	function get_qq_user_info_item_by_openid($openid)
	{
		if(!$openid)
		{
			return false;
		}
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER."get_qq_user_info_item_by_openid_{$openid}";
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$content = $this->get_alllist($this->table_name,1,1,"","openid='{$openid}' and status=1");
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		
		return $content[0];
		
	}
	//用户搜索
	function get_user_count_by_search($key="",$province="",$city="",$sex="",$verified_type="")
	{
		if($province)$where_array[] = "province = {$province}";
		if($city)$where_array[] = "city = {$city}";
		if($key)$where_array[] = "user_nickname like '{$key}%' ";
		if(strlen($sex)>0)$where_array[] = "sex = $sex";
		if(strlen($verified_type)>0)$where_array[] = "verified_type = $verified_type";
		$where_array[] = "status = 1";
		$where = join( " and " , $where_array);
		
		
		$count = $this->get_listcount($this->table_name,$where);
		
		
		return $count;
		
	}
	
	function get_user_list_by_search($p=1,$pcount =20,$key="",$province="",$city="",$sex="",$verified_type="")
	{
		if($province)$where_array[] = "province = {$province}";
		if($city)$where_array[] = "city = {$city}";
		if($key)$where_array[] = "user_nickname like '{$key}%' ";
		if(strlen($sex)>0)$where_array[] = "sex = $sex";
		if(strlen($verified_type)>0)$where_array[] = "verified_type = $verified_type";
		$where_array[] = "status = 1";
		$where = join( " and " , $where_array);
		
		$list = $this->get_alllist($this->table_name,$p,$pcount,"count_tuijian desc ",$where);
		
		if($list)
		{
			foreach($list as $key => $value)
			{
				$list[$key]["user_pic"] = "http://tp1.sinaimg.cn/{$value["uid"]}/180/5635936750/1";
			}
		}
		
		
		return $list;
	}
	
	/**
	 * 根据用户名取数据
	 * @param $user_name
	 * @param $password
	 */
	function get_user_item_by_name($user_name)
	{
		
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER."get_user_item_by_name_{$user_name}";
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$where = "user_name ='{$user_name}' and status = 1";
			$content = $this->get_alllist($this->table_name,1,1,"",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		if($content)
		{
			foreach($content as $key => $value)
			{
				$content[$key]["user_pic"] = "http://tp1.sinaimg.cn/{$value["uid"]}/180/5635936750/1";
			}
		}
		
		return $content ? $content[0] : false;
	}
	
		/**
	 * 根据用户uid取数据
	 * @param $user_name
	 * @param $password
	 */
	function get_user_list_by_uids($uids)
	{
		$uids = array_unique($uids);
			
		if(!$uids)
		{
			return false;
		}
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER."get_user_list_by_uids".join("_",$uids);
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$where = "uid in ( ".join(",",$uids)." ) and status = 1";
			$content = $this->get_alllist($this->table_name,"","","count_tuijian desc",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		if($content)
		{
			foreach($content as $key => $value)
			{
				$content[$key]["user_pic"] = "http://tp1.sinaimg.cn/{$value["uid"]}/180/5635936750/1";
			}
		}
		
		return $content;
	}
	
	
	
	
	/**
	 * 根据用户名取数据
	 * @param $user_name
	 * @param $password
	 */
	function get_user_list_by_names($user_name_array)
	{
		$user_name_array = array_unique($user_name_array);
		if(!$user_name_array)
		{
			return array();
		}
		$new_user_name_array = array();
		foreach($user_name_array as $key => $value)
		{
			$new_user_name_array[] = "'{$value}'";
		}
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER."get_user_list_by_names".join("_",$user_name_array);
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$where = "user_nickname in ( ".join(",",$new_user_name_array)." ) and status = 1";
			$content = $this->get_alllist($this->table_name,1,1,"",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		return $content;
	}
	
		
	
	/**
	 * 用户注册
	user_nickname 	char(20) 	utf8_unicode_ci 		否 	无 		Browse distinct values 	更改 	删除 	主键 	唯一 	索引 	全文搜索
	sex 	tinyint(1) 			否 	无 		Browse distinct values 	更改 	删除 	主键 	唯一 	索引 	全文搜索
	province 	tinyint(3) 			否 	无 		Browse distinct values 	更改 	删除 	主键 	唯一 	索引 	全文搜索
	city
	1 男 2女
	 * 
	 */
	function insert_qq_user_info($openid,$sex="",$user_nickname="",$province,$city,$company,$school,$verified_type,$bithday,$school_name,$start_year,$birthyear,$head_url,$link)
	{
		
		if(!$user_nickname)
		{
			return false;
		}
		// 插入数据
		$ary = array();
		$ary["openid"] = $openid;
		$ary["head_url"] = $head_url;
		
		$ary["sex"] = $sex;
		$ary["user_nickname"] = $user_nickname;
		$ary["province"] = $province;
		$ary["city"] = $city;
		$ary["company"] = $company;
		$ary["school_id"] = $school;
		$ary["school_name"] = $school_name;
		$ary["start_year"] = $start_year;
		$ary["verified_type"] = $verified_type;
		$ary["birthday"] = $bithday;
		$ary["birthyear"] = $birthyear;
		$ary["link"] = $link;
		$ary["update_time"] = Date::get_date_time();
		$ary["update_ip"] = IP::get_client_ip_long();
		$ary["status"] = 1;
		//$ary["is_local"] = 1;
		
		$ret = $this->insert($this->table_name, $ary,"",false);
		
		return $ret;
	}
	
	
	//更新用户信息
	function update_qq_user_info_by_uid($uid,$read_at_id=0,$user_pic="",$user_nickname="",$user_realname="",$province="",$sex="",$desc="",$new_password="")
	{
		$ary = array();

		if(strlen($read_at_id) > 0)
		{
			$ary["read_at_id"] = $read_at_id;
		}
		if(strlen($user_pic) > 0)
		{
			$ary["user_pic"] = $user_pic;
		}
		if(strlen($user_nickname) > 0)
		{
			$ary["user_nickname"] = $user_nickname;
		}
		if(strlen($user_realname) > 0)
		{
			$ary["user_realname"] = $user_realname;
		}
		
		if(strlen($province) > 0)
		{
			
			$ary["province"] = $province;
		}
		
		if(strlen($sex) > 0)
		{
			
			$ary["sex"] = $sex;
		}
		if(strlen($desc) > 0)
		{
			
			$ary["desc"] = $desc;
		}
		if(strlen($new_password) > 0)
		{
			
			$ary["password"] = $new_password;
		}
		
		
		
		$ary["update_time"] = Date::get_date_time();
		$ary["update_ip"] = IP::get_client_ip_long();
		
		$where = "uid = $uid";
		
		
		$ret = $this->update($this->table_name, $ary,$where);
		if ($ret)
		{
			$memcache = mem_cache::get_instance();
			$key = CACHE_PREFIX_USER."get_qq_user_info_item_by_uid_{$uid}";
			$memcache->delete($key);
		}
		else
		{
			return false;
		}


		return $ret;
		
		
		
	}
	
	function update_user_count_by_uid($uid,$count_love="",$count_album="",$count_mblog="",$count_video="",$count_text_mblog="")
	{
		$ary = array();

		$count_tuijian = 0;
		if(strlen($count_love) > 0)
		{
			$ary["count_love"] = "count_love+{$count_love}";
			if($count_love > 0)$count_tuijian ++;
		}
		
		if(strlen($count_album) > 0)
		{
			$ary["count_album"] = "count_album+{$count_album}";
			if($count_album > 0)$count_tuijian ++;
		}
		if(strlen($count_video) > 0)
		{
			$ary["count_video"] = "count_video+{$count_video}";
			if($count_video > 0)$count_tuijian ++;
		}
		if(strlen($count_mblog) > 0)
		{
			$ary["count_mblog"] = "count_mblog+{$count_mblog}";
			if($count_mblog > 0)$count_tuijian ++;
		}
		
		if(strlen($count_text_mblog) > 0)
		{
			$ary["count_text_mblog"] = "count_text_mblog+{$count_text_mblog}";
			if($count_mblog > 0)$count_tuijian ++;
		}
		
		
		if($count_tuijian)
		{
			$ary["count_tuijian"] = "count_tuijian+{$count_tuijian}";
		}
		
		
		
		$where = "uid = $uid";
		$ret = $this->update($this->table_name, $ary,$where);
	
		return $ret;
		
		
		
	}
	
	
	function get_user_order_by_count_tuijian_nocache($p,$pcount)
	{
		$where = "status = 1";
		$content = $this->get_alllist($this->table_name,$p,$pcount,"count_tuijian desc,update_time desc ",$where);
		
		return $content ;
	}
	

###################admin

	
	function admin_get_user_list_by_search($p=1,$pcount =20,$sex="",$verified_type="",$count_mblog="",$count_album="",$uid="",$status="",$site_id="")
	{
	
		if(strlen($sex)>0)$where_array[] = "sex = $sex  ";
		if(strlen($verified_type)>0)$where_array[] = "verified_type = $verified_type  ";
		if(strlen($count_mblog)>0)$where_array[] = "count_mblog >= $count_mblog  ";
		if(strlen($count_album)>0)$where_array[] = "count_album >= $count_album  ";
		if(strlen($uid)>0)$where_array[] = " uid =$uid  ";
		if(strlen($status)>0)$where_array[] = "status = $status";
		if(strlen($site_id)>0)$where_array[] = "main_topic_id = $site_id";
		$where = join( " and " , $where_array);
		
		$list = $this->get_alllist($this->table_name,$p,$pcount,"update_time desc ",$where);
		
		return $list;
	}
	
	
	function admin_get_user_count_by_search($sex="",$verified_type="",$count_mblog="",$count_album="",$uid="",$status="",$site_id="")
	{
	
		if(strlen($sex)>0)$where_array[] = "sex = $sex  ";
		if(strlen($verified_type)>0)$where_array[] = "verified_type = $verified_type  ";
		if(strlen($count_mblog)>0)$where_array[] = "count_mblog >= $count_mblog  ";
		if(strlen($count_album)>0)$where_array[] = "count_album >= $count_album  ";
		if(strlen($uid)>0)$where_array[] = " uid =$uid  ";
		if(strlen($status)>0)$where_array[] = "status = $status";
		if(strlen($site_id)>0)$where_array[] = "main_topic_id = $site_id";
		$where = join( " and " , $where_array);
		
		$list = $this->get_listcount($this->table_name,$where);
		
		return $list;
	}
	
	
	
	###########admin end
	
	
	
	function cron_insert_qq_user_info($uid,$sex="",$user_nickname="",$province,$city,$company,$school,$verified_type,$birthday,$desc)
	{
		if(!$uid)
		{
			return false;
		}
		// 插入数据
		$ary = array();
		$ary["uid"] = $uid;
		
		if($sex == "m")
		{
			$sex = 1;
		}
		else
		{
			$sex = 0;
		}
		$ary["sex"] = $sex;
		$ary["user_nickname"] = $user_nickname;
		$ary["province"] = $province;
		$ary["city"] = $city;
		$ary["company"] = $company;
		$ary["school"] = $school;
		$ary["desc"] = $desc;
		
		$ary["verified_type"] = $verified_type;
		$ary["birthday"] = $bithday;

		$ary["update_time"] = Date::get_date_time();
		$ary["update_ip"] = IP::get_client_ip_long();
		$ary["status"] = 1;
		$ary["is_local"] = 0;
		$ret = $this->insert($this->table_name, $ary);
		
		return $ret;
	}
	
	
	function cron_get_user_list_no_album($p=1,$pcount=100)
	{
		$where_array[] = "is_local = 0";//未激活
		$where_array[] = "count_mblog >= 4"; //抓取内容超过4条
		//$where_array[] = "count_album =0";//专辑个数为0
		$where_array[] = "status =1";
		$where = join(" and ",$where_array);
		
		
		$list = $ret = $this->get_alllist($this->table_name,$p,$pcount, "",$where);
		
		return $list;
	}
	
	function cron_get_user_count_no_album()
	{
		$where_array[] = "is_local = 0";//未激活
		$where_array[] = "count_mblog >= 4"; //抓取内容超过4条
		//$where_array[] = "count_album =0";//专辑个数为0
		$where_array[] = "status =1";
		$where = join(" and ",$where_array);
		
		
		$list = $ret = $this->get_listcount($this->table_name,$where);
		
		return $list;
	}
	
	
}



